import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/page/home/new/study_my_error_page.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_catar_select_page.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_select_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class StudyItemWidget extends StatelessWidget {
  final StudyModelItemBean itemBean;
  final double width;
  final double radius;
  final double pading;
  final int maxLines;

  StudyItemWidget(this.itemBean,
      {this.width = 44, this.radius = 8, this.pading = 0, this.maxLines = 2});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: buildClipRRect(itemBean),
      onTap: () {
        Widget page;

        if (itemBean.nodeType == "APPJUMP") {
          page = MyErrorPage();
        } else if (itemBean.nodeType == "APP-Native") {
          page = itemBean.page;
        } else {
          if (itemBean.nodeValue != null) {
            page = TopicSelectPage(
              pageTitle: "${itemBean.nodeName}",
              categoryOne: itemBean.nodeValue,
            );
          } else if (itemBean.nodeValues != null &&
              itemBean.nodeValues.length > 0) {
            page = TopicCatarSelectTwoPage(
                pageTitle: "${itemBean.nodeName}",
                nodeValues: itemBean.nodeValues);
          } else {
            //暂无详情
            ToastUtils.showToast("暂无详情");
            return;
          }
        }
        NavigatorUtils.pushPage(
          context,
          page,
        );
      },
    );
  }

  ClipRRect buildClipRRect(StudyModelItemBean itemBean) {
    return ClipRRect(
      child: Container(
        margin: EdgeInsets.only(left: 1, right: 1, bottom: 4),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              margin: EdgeInsets.only(left: 4, right: 4),
              padding: EdgeInsets.all(pading),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(radius),
                  )),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(radius),
                child: itemBean.nodeLogo != null
                    ? buildImageWidget(itemBean.nodeLogo)
                    : Image.asset(
                  "assets/images/2.0x/app_icon.png",
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 1),
              child: Text(
                "${itemBean.nodeName.replaceAll("#", "\n")}",
                maxLines: maxLines,
                textAlign: TextAlign.center,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    fontWeight: FontWeight.w500,
                    color: Colors.blueGrey,
                    fontSize: 14),
              ),
            ),
          ],
        ),
      ),
    );
  }

  buildImageWidget(String nodeLogo) {
    if (nodeLogo.startsWith("http") || nodeLogo.startsWith("https")) {
      return CachedNetworkImage(
        imageUrl: itemBean.nodeLogo,
        width: width,
      );
    }

    return Image.asset(
      itemBean.nodeLogo,
      width: width,
    );
  }
}
